<?php
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
class Token
{
    const SECRET = 'laohan';//密钥
    //创建token
    static public function create_token($uid = 1)
    {
        $payload = [
            'iss' => 'lh',                //签发人(官方字段:非必需)
            'exp' => time() + (60*60*24*7),     //过期时间(官方字段:非必需秒)
            'aud' => 'user',              //接收人(官方字段:非必需)
            'nbf' => time(),               //生效时间(官方字段:非必需)
            'iat' => time(),               //签发时间(官方字段:非必需)
            'userId' => $uid,        //自定义字段(用户id)
        ];
        $token = JWT::encode($payload, self::SECRET, 'HS256');

        return $token;
    }

    //验证token
    static public function verify_token($token)
    {
        try {
            $Result = JWT::decode($token, new Key(self::SECRET, 'HS256'));
            return true;
        } catch (\Exception $e) {
            return $e->getMessage();
            return false;
        }
    }


}
